<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="合同序号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入租赁合同序号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="续签合同序号" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入续签合同序号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="流程实例的编号" prop="processInstanceId">
        <el-input
          v-model="queryParams.processInstanceId"
          placeholder="请输入流程实例的编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="宗地序号" prop="landIds">
        <el-input
          v-model="queryParams.landIds"
          placeholder="请输入租赁宗地序号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所属用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入所属用户id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="出租金额" prop="rentPrice">
        <el-input
          v-model="queryParams.rentPrice"
          placeholder="请输入出租金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出租期限" prop="rentPeriod">
        <el-input
          v-model="queryParams.rentPeriod"
          placeholder="请输入出租期限"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="出租位置" prop="rentLocation">
        <el-input
          v-model="queryParams.rentLocation"
          placeholder="请输入出租位置"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="租赁土地面积" prop="rentLandarea">
        <el-input
          v-model="queryParams.rentLandarea"
          placeholder="请输入租赁土地面积"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="租赁建筑面积" prop="rentBuildingarea">
        <el-input
          v-model="queryParams.rentBuildingarea"
          placeholder="请输入租赁建筑面积"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出租方式" prop="rentType">
        <el-select
          v-model="queryParams.rentType"
          placeholder="请选择出租方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.RENT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="签订日期" prop="signingDate">
        <el-date-picker
          v-model="queryParams.signingDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="下次交款日期" prop="payDate">
        <el-date-picker
          v-model="queryParams.payDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="上次交款日期" prop="paidDate">
        <el-date-picker
          v-model="queryParams.paidDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item label="有效状态" prop="effective">
        <el-select
          v-model="queryParams.effective"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- 子表的列表 -->
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="rentFile">
            <el-tab-pane label="租赁合同档案" name="rentFile">
              <RentFileList :belong-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="宗地信息" name="land">
              <LandList :landIds="scope.row.landIds" />
            </el-tab-pane>
            <el-tab-pane label="年度收益" name="earnings">
              <EarningsList :rent-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="承租人信息" name="lessee">
              <LesseeList :rent-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="租金税费管理" name="taxation">
              <TaxationList :rent-id="scope.row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="选择" align="center" min-width="120px">
        <template #default="scope">
          <el-button type="primary" @click="handleChoose(scope.row)"> 确认 </el-button>
        </template>
      </el-table-column>
      <el-table-column label="租赁合同序号" align="center" prop="id" />
      <!-- <el-table-column label="续签合同序号" align="center" prop="parentId" /> -->
      <el-table-column label="流程实例的编号" align="center" prop="processInstanceId" />
      <el-table-column label="有效状态" align="center" prop="effective">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYS_STATUS" :value="scope.row.effective" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="填报人ID" align="center" prop="proposerId" /> -->
      <el-table-column label="租赁宗地序号" align="center" prop="landIds" />
      <!-- <el-table-column label="所属用户id" align="center" prop="userId" /> -->
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="出租金额" align="center" prop="rentPrice" />
      <el-table-column label="出租期限" align="center" prop="rentPeriod" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column label="出租位置" align="center" prop="rentLocation" />
      <el-table-column label="租赁土地面积" align="center" prop="rentLandarea" />
      <el-table-column label="租赁建筑面积" align="center" prop="rentBuildingarea" />
      <el-table-column label="出租方式" align="center" prop="rentType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.RENT_TYPE" :value="scope.row.rentType" />
        </template>
      </el-table-column>
      <el-table-column
        label="合同签订日期"
        align="center"
        prop="signingDate"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column
        label="约定交款日期"
        align="center"
        prop="payDate"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column
        label="上次交款日期"
        align="center"
        prop="paidDate"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column
        label="下次交款日期"
        align="center"
        prop="nextpayDate"
        :formatter="dateFormatter2"
        width="100px"
      />
      <el-table-column label="有效状态" align="center" prop="effective">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYS_STATUS" :value="scope.row.effective" />
        </template>
      </el-table-column>
      <el-table-column label="交费状态" align="center">
        <template #default="scope">
          <el-tag v-if="isPaid(scope.row.paidDate)" type="success">已交费</el-tag>
          <el-tag v-else type="danger">未交费</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <RentForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { RentApi, RentVO } from '@/api/zszx/rent'
import RentForm from './RentForm.vue'
import LesseeList from './components/LesseeList.vue'
import TaxationList from './components/TaxationList.vue'
import EarningsList from './components/EarningsList.vue'
import LandList from './components/LandList.vue'
import RentFileList from './components/RentFileList.vue'
/** 租赁合同管理 列表 */
defineOptions({ name: 'ChooseRent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<RentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  processInstanceId: undefined,
  effective: undefined,
  status: undefined,
  parentId: undefined,
  proposerId: undefined,
  landIds: undefined,
  userId: undefined,
  contractNo: undefined,
  rentPrice: undefined,
  rentPeriod: undefined,
  startTime: [],
  endTime: [],
  rentLocation: undefined,
  rentLandarea: [],
  rentBuildingarea: [],
  rentType: undefined,
  signingDate: [],
  payDate: [],
  paidDate: [],
  nextpayDate: [],
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

function isPaid(row) {
  //当前日期等于下次交款日期的所属年相同，则已缴
  if (!row) {
    return false
  }
  if (row.paidDate) {
    let lastPaidYear = new Date(row.paidDate).getFullYear()
    if (lastPaidYear == new Date().getFullYear()) {
      return true
    }
  } else if (row.nextpayDate == null) {
    return true
  } else {
    return false
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RentApi.getRentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RentApi.deleteRent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RentApi.exportRent(queryParams)
    download.excel(data, '租赁合同管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const emit = defineEmits(['rentSelected']) // 定义 success 事件，用于操作成功后的回调
/** 确认选择宗地按钮 */
function handleChoose(row) {
  emit('rentSelected', row)
}

/** 初始化 **/
onMounted(() => {
  nextTick(() => {
    getList()
  })
})
</script>
